//
//  LDSIFTTTEditViewController.m
//  LDSSmartHome
//
//  Created by leedarson on 2017/11/6.
//Copyright © 2017年 leedarson. All rights reserved.
//

#import "LDSIFTTTEditViewController.h"
#import "LDSIFTTTEditViewControllerViewModel.h"
#import "LDSIFTTTEditTableViewModel.h"
#import "LDSIFTTTEditHeadView.h"
@interface LDSIFTTTEditViewController ()

@property(nonatomic, strong) LDSIFTTTEditViewControllerViewModel *vcViewModel;//必须子类化
@property(nonatomic, strong) UITableView *tableView;
@property(nonatomic, strong) UIView *headView;
@property(nonatomic, strong) LDSIFTTTEditTableViewModel *tableViewModel;//选择性子类化
@property(nonatomic, strong) NSMutableArray *dataSource;
@property(nonatomic, strong) UIView *footView;
@end

@implementation LDSIFTTTEditViewController

#pragma mark - lazy init
- (LDSIFTTTEditViewControllerViewModel *)vcViewModel {
    if (_vcViewModel == nil) {
        _vcViewModel = [[LDSIFTTTEditViewControllerViewModel alloc] init];
        _vcViewModel.delegate = (id<LDSViewControllerViewModelDelegate>)self;
    }
    return _vcViewModel;
}

- (NSMutableArray *)dataSource {
    if (_dataSource == nil) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

- (UITableView *)tableView {
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStyleGrouped];
        _tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
        _tableView.tableHeaderView = self.headView;
        _tableView.tableFooterView = self.footView;
    }
    return _tableView;
}

- (UIView *)headView {
    if (_headView == nil) {
        _headView = [NSBundle.mainBundle loadNibNamed:@"LDSIFTTTEditHeadView" owner:self options:nil].lastObject;
        _headView.frame = CGRectMake(0, 0, self.view.frame.size.width, 150);
        _headView.backgroundColor = [UIColor lightTextColor];
    }
    return _headView;
}

- (UIView *)footView {
    if (_footView == nil) {
        _footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,100)];
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(15,0 ,self.view.frame.size.width - 30, 50);
        [btn setTitle:@"删除" forState:UIControlStateNormal];
        btn.backgroundColor = [UIColor redColor];
        btn.layer.masksToBounds = YES;
        btn.layer.cornerRadius = 25.0f;
        [_footView addSubview:btn];
    }
    return _footView;
}

- (LDSIFTTTEditTableViewModel *)tableViewModel {
    if (_tableViewModel == nil) {
        _tableViewModel = [[LDSIFTTTEditTableViewModel alloc] init];
        _tableViewModel.delegate = (id<LDSTableViewModelDelegate>)self;
        _tableViewModel.firstSectionFootViewHeight = 0.01f;
        _tableViewModel.otherSectionFootViewHeight = 26.0f;
        //tableViewModel接管tableView
        [_tableViewModel handleWithTable:self.tableView];
    }
    return _tableViewModel;
}

- (NSString *)barItemTitle {
    return @"";
}

#pragma mark - life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    //tableViewModel接管dataSource 数据源
    [self.tableViewModel handleWithDataSource:self.dataSource];
    [self.view addSubview:self.tableView];
    [self.vcViewModel loadFetchData];
}

#pragma mark - LDSTableViewModelDelegate
- (void)lds_tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    switch (indexPath.section) {
        case 0://if
            break;
        default://then
            break;
    }
}

#pragma mark - LDSViewControllerViewModelDelegate
- (void)lds_didReceiveDataWithDataSource:(NSArray *)dataSource andSectionDataSource:(NSArray *)sectionDataSource {
    self.dataSource = [dataSource mutableCopy];
    [self.tableViewModel handleWithDataSource:self.dataSource];
    [self.tableViewModel handleWithSectionDataSoure:[sectionDataSource mutableCopy]];
    [self.tableView reloadData];
}

@end
